package com.puris.service.admin;

import com.puris.dao.BaseDao;
import com.puris.dao.admin.*;
import com.puris.pojo.Admin;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Date;

/**
 * @author Sonata
 * @data 2021/11/26 18:49
 */
public class ServiceImpl implements Service {
    private AllStudentDao studentDao;
    private AllTeacherDao teacherDao;
    private AllCompetitionDao competitionDao;
    private AddStudentDao addStudentDao;
    private AddTeacherDao addTeacherDao;
    private AddCompetitionDao addCompetitionDao;
    private FindStudentDao findStudentDao;
    private FindTeacherDao findTeacherDao;
    private FindCompetitionDao findCompetitionDao;
    private UpdateStudentDao updateStudentDao;
    private UpdateTeacherDao updateTeacherDao;
    private UpdateCompetitionDao updateCompetitionDao;
    private DeleteStudentDao deleteStudentDao;
    private DeleteTeacherDao deleteTeacherDao;
    private DeleteCompetitionDao deleteCompetitionDao;

    public ServiceImpl() {
        studentDao = new AllStudentDaoImpl();
        teacherDao = new AllTeacherDaoImpl();
        competitionDao = new AllCompetitionDaoImpl();
        addStudentDao = new AddStudentDaoImpl();
        addTeacherDao = new AddTeacherDaoImpl();
        addCompetitionDao = new AddCompetitionDaoImpl();
        findStudentDao = new FindStudentDaoImpl();
        findTeacherDao = new FindTeacherDaoImpl();
        findCompetitionDao = new FindCompetitionDaoImpl();
        updateStudentDao = new UpdateStudentDaoImpl();
        updateTeacherDao = new UpdateTeacherDaoImpl();
        updateCompetitionDao = new UpdateCompetitionDaoImpl();
        deleteStudentDao = new DeleteStudentDaoImpl();
        deleteTeacherDao = new DeleteTeacherDaoImpl();
        deleteCompetitionDao = new DeleteCompetitionDaoImpl();
    }

    @Override
    public Admin login(String id, String password) {
        Admin admin = null;

        if ("puris".equals(id) && "sonata".equals(password)) {
            admin = new Admin("puris", "sonata");
        }

        return admin;
    }

    @Override
    public StringBuffer selectStudent() throws SQLException {
        Connection connection = null;
        connection = BaseDao.getConnection();

        StringBuffer student = null;

        student = studentDao.selectStudent(connection);

        BaseDao.closeResource(connection, null, null);

        return student;
    }

    @Override
    public boolean addStudent(String id, String department, int grade, String className, String studentName) throws SQLException {
        Connection connection = null;

        boolean flag = false;

        connection = BaseDao.getConnection();

        if (!addStudentDao.hasStudent(connection, id)) {
            flag = addStudentDao.addStudent(connection, id, department, grade, className, studentName);
        }

        BaseDao.closeResource(connection, null, null);

        return flag;
    }

    @Override
    public StringBuffer findStudent(String id) throws SQLException {
        Connection connection = null;
        connection = BaseDao.getConnection();

        StringBuffer student = null;

        student = findStudentDao.findStudent(connection, id);

        BaseDao.closeResource(connection, null, null);

        return student;
    }

    @Override
    public boolean updateStudent(String id, String department, int grade, String className, String studentName) throws SQLException {
        Connection connection = null;

        boolean flag = false;

        connection = BaseDao.getConnection();

        if (addStudentDao.hasStudent(connection, id)) {
            flag = updateStudentDao.updateStudentDao(connection, id, department, grade, className, studentName);
        }

        BaseDao.closeResource(connection, null, null);

        return flag;
    }

    @Override
    public boolean deleteStudent(String id) throws SQLException {
        Connection connection = null;

        connection = BaseDao.getConnection();

        if (deleteStudentDao.deleteStudent(connection, id)) {
            BaseDao.closeResource(connection, null, null);
            return true;
        } else {
            BaseDao.closeResource(connection, null, null);
            return false;
        }
    }

    @Override
    public StringBuffer selectTeacher() throws SQLException {
        Connection connection = null;
        connection = BaseDao.getConnection();

        StringBuffer teacher = null;

        teacher = teacherDao.selectTeacher(connection);

        BaseDao.closeResource(connection, null, null);

        return teacher;
    }

    @Override
    public boolean addTeacher(String id, String department, String teacherName, String competitionName) throws SQLException {
        Connection connection = null;

        boolean flag = false;

        connection = BaseDao.getConnection();

        if (!addTeacherDao.hasTeacher(connection, id)) {
            flag = addTeacherDao.addTeacher(connection, id, department, teacherName, competitionName);
        }

        BaseDao.closeResource(connection, null, null);

        return flag;
    }

    @Override
    public StringBuffer findTeacher(String id) throws SQLException {
        Connection connection = null;
        connection = BaseDao.getConnection();

        StringBuffer teacher = null;

        teacher = findTeacherDao.findTeacher(connection, id);

        BaseDao.closeResource(connection, null, null);

        return teacher;
    }

    @Override
    public boolean updateTeacher(String id, String department, String teacherName, String competitionName) throws SQLException {
        Connection connection = null;

        boolean flag = false;

        connection = BaseDao.getConnection();

        if (addTeacherDao.hasTeacher(connection, id)) {
            flag = updateTeacherDao.updateTeacher(connection, id, department, teacherName, competitionName);
        }

        BaseDao.closeResource(connection, null, null);

        return flag;
    }

    @Override
    public boolean deleteTeacher(String id) throws SQLException {
        Connection connection = null;

        connection = BaseDao.getConnection();

        if (deleteTeacherDao.deleteTeacher(connection, id)) {
            BaseDao.closeResource(connection, null, null);
            return true;
        } else {
            BaseDao.closeResource(connection, null, null);
            return false;
        }
    }

    @Override
    public StringBuffer selectCompetition() throws SQLException {
        Connection connection = null;
        connection = BaseDao.getConnection();

        StringBuffer competition = null;

        competition = competitionDao.selectCompetition(connection);

        BaseDao.closeResource(connection, null, null);

        return competition;
    }

    @Override
    public boolean addCompetition(String id, String competitionName, Date date, String describe, String teacherId, String teacherName) throws SQLException {
        Connection connection = null;

        boolean flag = false;

        connection = BaseDao.getConnection();

        if (!addCompetitionDao.hasCompetition(connection, id)) {
            flag = addCompetitionDao.addCompetition(connection, id, competitionName, date, describe, teacherId, teacherName);
        }

        BaseDao.closeResource(connection, null, null);

        return flag;
    }

    @Override
    public StringBuffer findCompetition(String id) throws SQLException {
        Connection connection = null;
        connection = BaseDao.getConnection();

        StringBuffer competition = null;

        competition = findCompetitionDao.findCompetition(connection, id);

        BaseDao.closeResource(connection, null, null);

        return competition;
    }

    @Override
    public boolean updateCompetition(String id, String competitionName, Date date, String describe, String teacherId, String teacherName) throws SQLException {
        Connection connection = null;

        boolean flag = false;

        connection = BaseDao.getConnection();

        if (addCompetitionDao.hasCompetition(connection, id)) {
            flag = updateCompetitionDao.updateCompetition(connection, id, competitionName, date, describe, teacherId, teacherName);
        }

        BaseDao.closeResource(connection, null, null);

        return flag;
    }

    @Override
    public boolean deleteCompetition(String id) throws SQLException {
        Connection connection = null;
        connection = BaseDao.getConnection();

        if (deleteCompetitionDao.deleteCompetition(connection, id)) {
            BaseDao.closeResource(connection, null, null);
            return true;
        } else {
            BaseDao.closeResource(connection, null, null);
            return false;
        }
    }
}
